package com.koron.css2.clientCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.BankBean;
import com.koron.css2.baseConfig.mapper.BankMapper;
import com.koron.css2.clientCenter.bean.AccountInfoBean;
import com.koron.css2.clientCenter.mapper.AccountInfoMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.clientCenter.queryBean.UserInfoMergeDetailQuery;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

/**
 * 账户管理-根据客户号码查询所属的账户列表
 *
 * @author 朱超
 * @since 2022-04-01 10:44:39
 */
public class GetAccountListByCtmNo implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            UserInfoMapper userInfoMapper = factory.getMapper(UserInfoMapper.class);
            AccountInfoMapper accountInfoMapper = factory.getMapper(AccountInfoMapper.class);
            BankMapper bankMapper = factory.getMapper(BankMapper.class);

            UserInfoMergeDetailQuery userInfoMergeDetailQuery = JsonUtils.objectToPojo(req.getData(),UserInfoMergeDetailQuery.class);

            // 1. 根据ctmNo查询所有的accountNoList
            List<String> accountNoList = userInfoMapper.getAccountNoListByCtmNo(userInfoMergeDetailQuery.getCtmNo());
            // 1.1 兼容客户对应的账户为空的情况
            if (accountNoList != null && accountNoList.size() == 0) {
                return MessageBean.create(Constant.MESSAGE_INT_FAIL,"该客户下没有对应在用的账户",null);
            }
            // 2. 根据accountNoList 查询对应的accountInfoBeanList
            List<AccountInfoBean> accountInfoBeanList = accountInfoMapper.getAccountsByAccountNos(accountNoList);
            // 2.1 获取开户行名称;设置增值税标记名称
            for (AccountInfoBean accountInfoBean:accountInfoBeanList) {
                if (StringUtils.isNotBlank(accountInfoBean.getOpenAccountBank())) {
                    BankBean bankName = bankMapper.findBankById(accountInfoBean.getOpenAccountBank());
                    accountInfoBean.setOpenAccountBankName(bankName == null ? "" : bankName.getBankName());
                }
                if(StringUtils.isNotBlank(accountInfoBean.getVatFlag()) && "1".equals(accountInfoBean.getVatFlag())) {
                    accountInfoBean.setVatFlagName("是");
                }else {
                    accountInfoBean.setVatFlagName("否");
                }
            }
            // 3. 返回数据
            MessageBean<List> messageBean = MessageBean.create(Constant.MESSAGE_INT_SUCCESS,"账户管理-根据客户号码查询所属的账户列表成功",List.class);
            messageBean.setData(accountInfoBeanList);
            return messageBean;
        } catch (Exception e) {
            logger.error("账户管理-根据客户号码查询所属的账户列表失败",e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL,"账户管理-根据客户号码查询所属的账户列表失败",null);
        }
    }
}
